package org.jeecg.modules.crm.defa.service.impl;

import org.jeecg.modules.crm.defa.entity.DefaAaOrder;
import org.jeecg.modules.crm.defa.entity.DefaAaOrderProduct;
import org.jeecg.modules.crm.defa.entity.DefaAaOrderAddress;
import org.jeecg.modules.crm.defa.mapper.DefaAaOrderProductMapper;
import org.jeecg.modules.crm.defa.mapper.DefaAaOrderAddressMapper;
import org.jeecg.modules.crm.defa.mapper.DefaAaOrderMapper;
import org.jeecg.modules.crm.defa.service.IDefaAaOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 水果订单表
 * @Author: jeecg-boot
 * @Date:   2024-07-31
 * @Version: V1.0
 */
@Service
public class DefaAaOrderServiceImpl extends ServiceImpl<DefaAaOrderMapper, DefaAaOrder> implements IDefaAaOrderService {

	@Autowired
	private DefaAaOrderMapper defaAaOrderMapper;
	@Autowired
	private DefaAaOrderProductMapper defaAaOrderProductMapper;
	@Autowired
	private DefaAaOrderAddressMapper defaAaOrderAddressMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(DefaAaOrder defaAaOrder, List<DefaAaOrderProduct> defaAaOrderProductList,List<DefaAaOrderAddress> defaAaOrderAddressList) {
		defaAaOrderMapper.insert(defaAaOrder);
		if(defaAaOrderProductList!=null && defaAaOrderProductList.size()>0) {
			for(DefaAaOrderProduct entity:defaAaOrderProductList) {
				//外键设置
				entity.setOrderId(defaAaOrder.getId());
				defaAaOrderProductMapper.insert(entity);
			}
		}
		if(defaAaOrderAddressList!=null && defaAaOrderAddressList.size()>0) {
			for(DefaAaOrderAddress entity:defaAaOrderAddressList) {
				//外键设置
				entity.setOrderId(defaAaOrder.getId());
				defaAaOrderAddressMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(DefaAaOrder defaAaOrder,List<DefaAaOrderProduct> defaAaOrderProductList,List<DefaAaOrderAddress> defaAaOrderAddressList) {
		defaAaOrderMapper.updateById(defaAaOrder);
		
		//1.先删除子表数据
		defaAaOrderProductMapper.deleteByMainId(defaAaOrder.getId());
		defaAaOrderAddressMapper.deleteByMainId(defaAaOrder.getId());
		
		//2.子表数据重新插入
		if(defaAaOrderProductList!=null && defaAaOrderProductList.size()>0) {
			for(DefaAaOrderProduct entity:defaAaOrderProductList) {
				//外键设置
				entity.setOrderId(defaAaOrder.getId());
				defaAaOrderProductMapper.insert(entity);
			}
		}
		if(defaAaOrderAddressList!=null && defaAaOrderAddressList.size()>0) {
			for(DefaAaOrderAddress entity:defaAaOrderAddressList) {
				//外键设置
				entity.setOrderId(defaAaOrder.getId());
				defaAaOrderAddressMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		defaAaOrderProductMapper.deleteByMainId(id);
		defaAaOrderAddressMapper.deleteByMainId(id);
		defaAaOrderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			defaAaOrderProductMapper.deleteByMainId(id.toString());
			defaAaOrderAddressMapper.deleteByMainId(id.toString());
			defaAaOrderMapper.deleteById(id);
		}
	}
	
}
